package com.sunwhite.herostore.cmdhandler;


import com.sunwhite.herostore.common.BroadCaster;
import com.sunwhite.herostore.common.UserManager;
import com.sunwhite.herostore.entity.User;
import com.sunwhite.herostore.msg.GameMsgProtocol;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;

public class UserEntryCmdHandler implements CmdHandler<GameMsgProtocol.UserEntryCmd> {
    @Override
    public void handle(ChannelHandlerContext channelHandlerContext, GameMsgProtocol.UserEntryCmd msg) {

        // 从信道到获得 userId Channel
        Integer userId = (Integer) channelHandlerContext.channel().attr(AttributeKey.valueOf("userId")).get();
        User user = UserManager.getUserById(userId);
        if (null==user){
            return;
        }
        // 构建结果并发送
        GameMsgProtocol.UserEntryResult.Builder resultBuilder = GameMsgProtocol.UserEntryResult.newBuilder();
        resultBuilder.setUserId(userId);
        resultBuilder.setUserName(user.userName);
        resultBuilder.setHeroAvatar(user.heroAvatar);
        GameMsgProtocol.UserEntryResult newResult = resultBuilder.build();
        BroadCaster.broadCast(newResult);
    }
}
